package com.rocketmq.aliyun.consume;

import org.apache.rocketmq.client.AccessChannel;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @author zhangyan
 * @version $ Id: Consumer.java, v 0.1 2021/5/24 19:42 zhangyan Exp $
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //设置为您在阿里云消息队列RocketMQ版控制台上创建的GID, 以及替换为您阿里云账号的AccessKey ID和AccessKey Secret。
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("TransactionGroup", true);
        //设置为阿里云消息队列RocketMQ版实例的接入点。
        consumer.setNamesrvAddr("127.0.0.1:9876");
        //阿里云上消息轨迹需要设置为CLOUD方式。
        consumer.setAccessChannel(AccessChannel.LOCAL);
        // 设置为您在阿里云消息队列RocketMQ版控制台上创建的Topic。
        consumer.subscribe("TransactionTopic", "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                                                            ConsumeConcurrentlyContext context) {
                System.out.println("====================: %s %n" + msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }
}
